package com.group4.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group4.rbac.entity.Role;
import com.group4.rbac.entity.RolePermission;
import com.group4.rbac.mapper.RoleMapper;
import com.group4.rbac.mapper.RolePermissionMapper;
import com.group4.rbac.service.RolePermissionService;
import com.group4.rbac.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group4.rbac.vo.RolePermissionVO;
import com.study.util.ResponseResult;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 添加角色业务逻辑层
     * @param permissionVO
     * @return
     */
    @Transactional
    @Override
    public ResponseResult add(RolePermissionVO permissionVO) {
        log.info("角色信息:{}",permissionVO.getRole());
        if(permissionVO.getRole() == null || permissionVO.getRole().getRoleName() == null || permissionVO.getRole().getRoleName().trim().length()==0 || permissionVO.getRole().getRoleCode()==null || permissionVO.getRole().getRoleCode().trim().length()==0){
            return new ResponseResult(500,"添加信息不能为空");
        }
        //查询角色名是否存在
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("name",permissionVO.getRole().getRoleName());
        Role role1 = roleMapper.selectOne(qw);
        if(role1 != null){
            return new ResponseResult(502,"该角色已经存在");
        }
        permissionVO.getRole().setGmtCreate(new Date());
        permissionVO.getRole().setGmtModified(new Date());
        //添加角色
        int insert = roleMapper.insert(permissionVO.getRole());
        //判断是否添加成功
        if(insert == 0){
            return new ResponseResult(501,"添加信息失败");
        }

        //为角色添加权限，角色权限中间表
        RolePermission rolePermission = new RolePermission();

        //一个角色可能有多个权限
        for(String p : permissionVO.getPermissionId()){
            rolePermission.setPermissionId(p);
            rolePermission.setRoleId(role1.getId());
            rolePermission.setIsDeleted(0);
            rolePermission.setGmtCreate(new Date());
            rolePermission.setGmtModified(new Date());
            rolePermissionMapper.insert(rolePermission);
        }

        return new ResponseResult(200,"添加角色成功");
    }

        /**
         * 修改角色信息
         * @param role
         * @return
         */
        @Override
        public ResponseResult update(Role role) {
            if(role==null|| role.getId() == null || role.getId().trim().length()==0){
                return new ResponseResult(500,"角色id为空");
            }
            int i = roleMapper.updateById(role);
            if(i == 0){
                return new ResponseResult(501,"修改失败");
            }
            QueryWrapper<Role> qw = new QueryWrapper<>();
            qw.eq("id",role.getId());
            Role role1 = roleMapper.selectOne(qw);
            return new ResponseResult(200,"修改成功",role1);
        }
}
